﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;

namespace WebPresentation
{
    public partial class Recipients : System.Web.UI.Page
    {
        int original_depID;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UserID"] == null)
                {
                    Response.Redirect("LoginPage.aspx");
                }
                if (!Session["Role"].Equals("Admin"))
                {
                    Response.Redirect("DocumentUser.aspx");
                }
                if (Request.QueryString.Count > 0)
                {
                    ddldocid.DataBind();
                    ddldocid.SelectedValue = Request.QueryString["docID"];
                    Label10.Text = "Please add recipient to the document.";
                }
            }       
        }

        protected void ImageButtonadd_Click(object sender, ImageClickEventArgs e)
        {
            int docID = Convert.ToInt32(ddldocid.SelectedValue);
            int depID = Convert.ToInt32(ddldepid.SelectedValue);
            if (new RecipientBO().AddNewRecipient(docID, depID) > 0)
            {
                Label10.Text = "Add successfully.";
                dtgrecipients.DataBind();
            }
            else
            {
                Label10.Text = "Add failed";
            }
        }

        protected void ImageButtonDelete_Click(object sender, ImageClickEventArgs e)
        {
            int docID = Convert.ToInt32(dtgrecipients.SelectedRow.Cells[1].Text);
            int depID = Convert.ToInt32(dtgrecipients.SelectedRow.Cells[2].Text);
            if (dtgrecipients.SelectedRow != null)
            {
                if (new RecipientBO().DeleteRecipient(docID, depID) > 0)
                {
                    Label10.Text = "Delete successfully";
                    dtgrecipients.DataBind();
                }
                //else
                //{
                //    Label10.Text = "Delete failed";
                //}
            }
        }

        protected void ImageButtonedit_Click(object sender, ImageClickEventArgs e)
        {
            int oldDocID = Convert.ToInt32(dtgrecipients.SelectedRow.Cells[1].Text);
            int oldDepID = Convert.ToInt32(dtgrecipients.SelectedRow.Cells[2].Text);
            int docID = Convert.ToInt32(ddldocid.SelectedValue);
            int depID = Convert.ToInt32(ddldepid.SelectedValue);
            if (dtgrecipients.SelectedRow != null)
            {
                if (new RecipientBO().DeleteRecipient(oldDocID, oldDepID) > 0 && new RecipientBO().AddNewRecipient(docID, depID) > 0)
                {
                    Label10.Text = "Edit successfully";
                    dtgrecipients.DataBind();
                }
                else
                {
                    Label10.Text = "Edit failed";
                }
            }
        }

        protected void dtgrecipients_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddldocid.SelectedValue = dtgrecipients.SelectedRow.Cells[1].Text;
            ddldepid.SelectedValue = dtgrecipients.SelectedRow.Cells[2].Text;
        }
    }
}